package com.welike.shibo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welike.shibo.eneity.Fans;
import com.welike.shibo.eneity.User;
import com.welike.shibo.mapper.FansMapper;
import com.welike.shibo.service.FansService;
import com.welike.shibo.service.UserService;
import com.welike.shibo.vo.page.FansPageVo;
import com.welike.shibo.vo.page.StarPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-20 00:40
 */
@Service
public class FansServiceImpl extends ServiceImpl<FansMapper, Fans> implements FansService {

    @Autowired
    private FansMapper fansMapper;

    @Autowired
    private UserService userService;

    /**
     * 查询关注了哪些人
     *
     * @param fanId <= uid 把自己作为fanId
     * @return List
     */
    @Override
    public List<String> queryUserIdByFanId(String fanId) {
        return fansMapper.queryUserIdByFanId(fanId);
    }

    /**
     * 查询有哪些粉丝
     *
     * @param userId <= uid 把自己作为userId
     * @return List
     */
    @Override
    public List<String> queryFanIdByUserId(String userId) {
        return fansMapper.queryFanIdByUserId(userId);
    }

    // 查询用户(uid)的粉丝总数
    @Override
    public Integer countByUserId(String uid) {
        return fansMapper.countByUserId(uid);
    }

    // 查询用户(uid=>fanId)的关注总数
    @Override
    public Integer countByFanId(String fanId) {
        return fansMapper.countByFanId(fanId);
    }

    // 通过uid和fanId查询是否存在关注关系
    @Override
    public String queryIdByUserIdAndFanId(String uid, String fanId) {
        return fansMapper.queryIdByUserIdAndFanId(uid, fanId);
    }

    // 查询自己关注的所有人
    @Override
    public List<StarPageVo> queryFollowStarPage(Integer page, Integer pageSize, String uid) {
        List<StarPageVo> starPageVoList = new ArrayList<>();
        // 首先查询所有的自己关注的人
        page = (page - 1) * pageSize; // 计算page
        List<String> FollowList = fansMapper.queryUserIdByFanIdForPage(page, pageSize, uid);
        for (String followUserId : FollowList) {
            StarPageVo starPageVo = new StarPageVo();
            // 查询关注的人(作为粉丝followUserId)和自己(被关注)的关系
            String result = this.queryIdByUserIdAndFanId(uid, followUserId);
            if (result != null) {
                // 如果不为空，则是互相关注的关系
                starPageVo.setFollowStatus(true);
            } else {
                starPageVo.setFollowStatus(false);
            }
            User user = userService.queryUserByUid(followUserId);
            starPageVo.setUid(followUserId);
            starPageVo.setNickName(user.getNickname());
            starPageVo.setAvatar(user.getAvatar());
            starPageVoList.add(starPageVo);
        }
        return starPageVoList;
    }

    @Override
    public List<FansPageVo> queryFansPage(Integer page, Integer pageSize, String uid) {
        List<FansPageVo> fansPageVoList = new ArrayList<>();
        // 首先查询所有的自己的粉丝
        page = (page - 1) * pageSize; // 计算page
        List<String> fansList = fansMapper.queryFanIdByUserIdForPage(page, pageSize, uid);
        for (String fansId: fansList){
            FansPageVo fansPageVo = new FansPageVo();
            // 查询粉丝(fanId)和自己(关注)的关系
            String result = this.queryIdByUserIdAndFanId(fansId, uid);
            if (result != null){
                // 如果不为空，则是互相关注的关系
                fansPageVo.setFollowStatus(true);
            } else {
                fansPageVo.setFollowStatus(false);
            }
            User user = userService.queryUserByUid(fansId);
            fansPageVo.setUid(fansId);
            fansPageVo.setNickName(user.getNickname());
            fansPageVo.setAvatar(user.getAvatar());
            fansPageVo.setFollowDate(new Date());
            fansPageVoList.add(fansPageVo);
        }
        return fansPageVoList;
    }


}